<?php
namespace Modules\ApiV2\Controllers;
use App\Models\Activity;
use App\Models\Advs;
use App\Models\Banner;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\Service\Logic\HomeLogic;
use Modules\Service\Logic\UserLogic;

class ActivityController extends BaseController{

    const PATH = "http://online.api.wenhuakj.com/v2/";

//    public function activityAll()
//    {
//        $arr1 = array('http://h5.jiayouhui360.com/img/hyfl01.87a1092c.png','http://download.jiayouhui360.com/hd_qingmingjie.png','http://h5.jiayouhui360.com/img/hyfl02.215b5720.png','http://h5.jiayouhui360.com/img/hyfl03.24b82c86.png','http://download.jiayouhui360.com/hb_20190305163745.png');
//        $arr2 = array('邀请有礼，多邀多得','清明踏青,惠有壕礼','双十一任性购，红包送不停','国庆出游季','喜迎中秋','');
//        $arr3 = array('http://h5.jiayouhui360.com/Share','','','','');
//        $arr4 = array('进行中','已结束','已结束','已结束','已结束');
//        $d = [];
//        foreach($arr1 as $key=>$val){
//            $d[$key]['id'] = $key+1;
//
//            $d[$key]['image']=$arr1[$key];
//
//            $d[$key]['title']=$arr2[$key];
//
//            $d[$key]['url']=$arr3[$key];
//
//            if ($arr4[$key]=='进行中'){
//                $d[$key]['status']=1;
//                if ($key==0) {
//                    $d[$key]['redirect_to'] = 'Share';
//                }else {
//                    $d[$key]['redirect_to'] = 'lottery';
//                }
//            }else{
//                $d[$key]['status']=0;
//                $d[$key]['redirect_to'] = '';
//            }
//
//            $d[$key]['status_text'] = $arr4[$key];
//        }
//        //查询广告
//        $bannerArray['welfare1'] = Advs::advsImage("welfare1");
//        $bannerArray['welfare2'] = Advs::advsImage("welfare2")[0];
//        $bannerArray['welfare3'] = Advs::advsImage("welfare3")[0];
//        $array = ["activity"=>$d,"welfare"=>$bannerArray];
//        return self::success($array);
//    }

    //福利中心
    public function activityAllList(){
        try {
            //查询活动
            $activityData = Activity::orderBy('sort', 'desc')->select("id","title","image_url","redirect_to","h5_url","status")->get();
            $activity = [];
            if ($activityData->count()>0){
                foreach ($activityData as $item){
                    if ($item->status==1){
                        $status_text = "进行中";
                    }else{
                        $status_text = "已结束";
                    }
                    $activity[] = [
                        "id" => $item->id,
                        "title" => $item->title,
                        "image" => $item->image_url,
                        "redirect_to" => $item->redirect_to,
                        "url" => $item->h5_url,
                        "status" => $item->status,
                        "status_text" => $status_text
                    ];
                }
            }
            $array = ["activity"=>$activity];
            return self::success($array);
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,'系统错误');
        }
    }


    //新闻资讯
    public function newsInformationList(Request $request){
        //接收参数
        $page = $request->input("page",1);
        $size = $request->input("limit",5);
        $platform = $request->header('app-platform');
        $version = $request->header('app-version');
        $bundleId = $request->header('bundle_id','youhuixia');
        return HomeLogic::news($page,$size,self::PATH,$version,$platform,$bundleId);
    }

    //新闻资讯详情
    public function newDetail($id){
        try{
            //查询资讯详情
            $newData = News::where("id",$id)->select(['title','detail'])->first();
            return view('detail',['page_title'=>$newData->title,'detail'=>$newData->detail]);
        }catch (\Exception $e){
            Log::error($e);
            return self::fail(500,"系统错误");
        }
    }

    //今日油价
    public function todayOilPrice(Request $request){
        $province = $request->input("province",'浙江省');
        return UserLogic::todayOilPrice($province);
    }
}